#include "main_page.h"

using std::cout;
using std::endl;
using std::fstream;
using std::list;
using std::string;
using std::istringstream;
using std::getline;
using std::stoi;

MainPage::MainPage(int width, int height, int page_num, int totle_price): Pages(width, height) {
        this->m_page_num = page_num;
        this->m_totle_price = totle_price;
}

// void MainPage::display(){

// }

void MainPage::lists_init(){
    
    fstream file;
    file.open("./res/dish_infomations.txt");
    if(!file.is_open()){
        cout << "file not open for" << "dish_infomations.txt" << endl;
        return;
    }
    cout << "file open for" << "dish_infomations.txt" << endl;
    // list<DishType> dish_type_list;

    while(!file.eof()){
        char buf[1024] = {0};
        file.getline(buf, sizeof(buf));

        string tmp(buf);

        string dish_type = tmp.substr(0, tmp.find(":"));
        string value = tmp.substr(tmp.find(":")+1);

        //创建对象并放在链表中
        DishType new_dish_type(dish_type);
        
        int start = 0;
        size_t commaPos = value.find(",");
        while(commaPos != string::npos){
            string dish_name = value.substr(start, commaPos-start);
            size_t openParenPos = dish_name.find("(");
            size_t closeParenPos = dish_name.find(")");
            
            size_t bmpOpenPos = dish_name.find("[");
            size_t bmpClosePos = dish_name.find("]");
            
            if(openParenPos != string::npos&&closeParenPos!=string::npos){
                string name = dish_name.substr(0, openParenPos);
                int price = stoi(dish_name.substr(openParenPos+1, closeParenPos-openParenPos-1));
                
                string bmp_path = dish_name.substr(bmpOpenPos+1, bmpClosePos-bmpOpenPos-1);
                
                //cout << name << price << endl;
                Dish new_dish(name, price,bmp_path);
                new_dish_type.add_dish(new_dish);
            }

            start = commaPos+1;
            commaPos = value.find(",", start);
        }
        // 处理最后一个元素
        if (!value.empty()) {
            std::string dishName = value.substr(start);
            size_t openParenPos = dishName.find('(');
            size_t closeParenPos = dishName.find(')');

            size_t bmpOpenPos = dishName.find("[");
            size_t bmpClosePos = dishName.find("]");

            if (openParenPos != std::string::npos && closeParenPos != std::string::npos) {
                std::string name = dishName.substr(0, openParenPos);
                int price = std::stoi(dishName.substr(openParenPos + 1, closeParenPos - openParenPos - 1));
                
                string bmp_path = dishName.substr(bmpOpenPos+1, bmpClosePos-bmpOpenPos-1);
                
                //cout << name << price << endl;
                Dish new_dish(name, price, bmp_path);
                new_dish_type.add_dish(new_dish);
            }
        }
        dish_type_list.push_back(new_dish_type);
    }
    file.close();

}

void MainPage::dish_name_price_init(){
    DishType first_dish_type = dish_type_list.front();
    list<Dish>::iterator it;
    int frameToLcdPosX = 24;
    int frameToLcdPosY = 75;
    int frameToLcdPosX_price =50;
    int frameToLcdPosy_price =200;
    int i = 0;
    for(it = first_dish_type.begin(); it != first_dish_type.end(); ++it){       
        if(i < 8){            
            font_show((*it).get_name().c_str(),26,170,30,0xffffffff,0,0,0x00000000,frameToLcdPosX+196*(i%4),frameToLcdPosY+180*(i/4));          
            string price = std::to_string((*it).get_price());       
            font_show(price.c_str(),30,55,28,0xffffffff,0,0,0x00000000,frameToLcdPosX_price+196*(i%4),frameToLcdPosy_price+180*(i/4));           
        }  
        i++;
    }
}

void MainPage::lists_show(){
    list<DishType>::iterator it;
    for(it = dish_type_list.begin(); it != dish_type_list.end(); ++it){
        DishType& dish_type = *it;
        dish_type.print_dish_type();
        dish_type.print_dish();
    }
}


int MainPage::dish_vector_init(Dish_Type_E flag, int page_num){
    //存数据前先清空dish_vector中的数据
    dish_vector.clear();
    // 获取第flag+1个元素
    int i = 0;
    list<DishType>::iterator it_dt = dish_type_list.begin();
    //cout << "flag:" << int(flag) << endl;
    std::advance(it_dt, int(flag)); // 移动到第flag+1个元素
    //cout << "flag:" << int(flag) << endl;
    cout << (*it_dt).getdishes().size() << endl;
    if(page_num <= ((*it_dt).getdishes().size()/8)+1){
        list<Dish>:: iterator it_d = (*it_dt).begin();
        std::advance(it_d, (page_num - 1) * 8);  // 移动迭代器
        for(it_d ; it_d != (*it_dt).end(); ++it_d){       
            if(i < 8){            
                dish_vector.push_back(*it_d);
            }
            i++;
        }
        cout << "dish_vector->size():" <<dish_vector.size()<< endl;
        return i>8?8:i;
    }
    

}

void MainPage::dish_vector_show(int num){
    // 使用范围基的for循环遍历数组中的每个vector
    int i = 0;
    int frameToLcdPosX = 24;
    int frameToLcdPosY = 75;
    int frameToLcdPosX_price =50;
    int frameToLcdPosy_price =200;
    int flag = 0;
    std::vector<Dish>::iterator it;
    for(it = dish_vector.begin(); (i < num) &&(it != dish_vector.end()); ++it){    
        //if(i < num){    
            // if((*it).get_name().length() < 1){
            //     break;
            // }  
            font_show((*it).get_name().c_str(),26,170,30,0xffffffff,0,0,0x00000000,frameToLcdPosX+196*(i%4),frameToLcdPosY+180*(i/4));          
            
            //展示菜品图片
            string path = (*it).get_path();
            //cout << path << endl;
            Bmp bmp(path.c_str());
            bmp.show(23 + 196*(flag%4), 112 + 180*(flag/4));

            
            string price = std::to_string((*it).get_price());        
            font_show(price.c_str(),30,55,28,0xffffffff,0,0,0x00000000,frameToLcdPosX_price+196*(i%4),frameToLcdPosy_price+180*(i/4));           
        //}  
        flag++;
        i++;
    }

}



void MainPage::dish_type_click(Dish_Type_E type){
    //初始化主页面
    Bmp bmp("./res/interfaces/main_interface.bmp");
    //存储某个菜种类的菜品数量
    int i = 0;
    switch(type){
        case Dish_Type_E::Explosive_package:
            cout<< "点击了爆款套餐"<< endl;
            this->m_page_num = 1;
            i = dish_vector_init(Dish_Type_E::Explosive_package, m_page_num);
            bmp.show();
            dish_vector_show(i);
            break;
        case Dish_Type_E::Nanchang_mixed_rice_noodles:
            cout<< "点击了南昌拌粉"<< endl;
            this->m_page_num = 1;
            i = dish_vector_init(Dish_Type_E::Nanchang_mixed_rice_noodles, m_page_num);
            cout << "i = " << i << endl;
            bmp.show();
            dish_vector_show(i);
            break;
        case Dish_Type_E::clay_pot_soup:
            cout<< "点击了瓦罐汤"<< endl;
            this->m_page_num = 1;
            i = dish_vector_init(Dish_Type_E::clay_pot_soup, m_page_num);
            bmp.show();
            dish_vector_show(i);
            break;
        case Dish_Type_E::Nanchang_snacks:
            cout<< "点击了南昌小吃"<< endl;
            this->m_page_num = 1;
            i = dish_vector_init(Dish_Type_E::Nanchang_snacks, m_page_num);
            bmp.show();
            dish_vector_show(i);
            break;
        case Dish_Type_E::beverage:
            cout<< "饮料酒水"<< endl;
            this->m_page_num = 1;
            i = dish_vector_init(Dish_Type_E::beverage, m_page_num);
            bmp.show();
            dish_vector_show(i);
            break;
        default:
            break;
    }
}

//点击下方上一页下一页的按钮
void MainPage::prev_next_click(Dish_Type_E dish_type, Page_Type_E page_type){
    //初始化主页面
    Bmp bmp("./res/interfaces/main_interface.bmp");
    //存储某个菜种类的菜品数量
    int i = 0;
    switch(page_type){
        case Page_Type_E::Prev_page:
            cout<< "点击了上一页"<< endl;
            this->m_page_num--;
            i = dish_vector_init(dish_type, m_page_num);
            bmp.show();
            dish_vector_show(i);
            break;
        case Page_Type_E::Next_page:
            cout<< "点击了下一页"<< endl;
            this->m_page_num++;
            i = dish_vector_init(dish_type, m_page_num);
            bmp.show();
            dish_vector_show(i);
            break;
    }
}

void MainPage::add_to_shopping_cart(int i){
    cout << "点击了第" << i+1 << "个位置的+号" << endl;
    //遍历std::list<ShopDish> m_shopping_cart,如果没有相同的菜就push_back，有就数量+1
    for(auto it = this->m_shopping_cart.begin(); it != this->m_shopping_cart.end(); it++){
        if(it->get_dish().get_name() == this->dish_vector[i].get_name()){
            it->add_num();
            return;
        }
    }
    cout << this->dish_vector.empty()<< endl;
    //提取出std::vector<Dish> dish_vector[8]第i个位置的菜
    cout << this->dish_vector[i].get_name() << endl;
    cout << this->dish_vector[i].get_price() << endl;
    Dish dish = this->dish_vector[i];
    //添加到std::list<ShopDish> m_shopping_cart中
    this->m_shopping_cart.push_back(ShopDish(dish));
}

void MainPage::minus_from_shopping_cart(int i){
    cout << "点击了第" << i+1 << "个位置的-号" << endl;
    //遍历std::list<ShopDish> m_shopping_cart,如果有相同的菜就数量-1，没有就不做处理
    for(auto it = this->m_shopping_cart.begin(); it != this->m_shopping_cart.end(); it++){
        if(it->get_dish().get_name() == this->dish_vector[i].get_name()){
            it->minus_num();
            //如果数量减为0，就把这个菜移除std::list<ShopDish> m_shopping_cart
            if(it->get_num() == 0){
                this->m_shopping_cart.erase(it);
            }
            return;
        }
    }
}

void MainPage::totle_price_calculate(){
    m_totle_price = 0;
    for(auto it = this->m_shopping_cart.begin(); it != this->m_shopping_cart.end(); it++){
        m_totle_price += it->get_dish().get_price() * it->get_num();
    }
}


int MainPage::get_total_price(){
    return m_totle_price;
}

void MainPage::totle_price_show(){
    string total_price_str = std::to_string(m_totle_price);
    font_show(total_price_str.c_str(),26,100,30,0xffffffff,0,0,0x00000000,100,435);

}

void MainPage::order_click(){
    cout << "---------------------------------------------"<<endl;
    cout << "点击了下单按钮" << endl;
    if(m_shopping_cart.empty()){
        cout << "购物车为空" << endl;
        return;
    }
    //遍历m_shopping_cart，将菜名以及数量输出到控制台
    for(auto it = this->m_shopping_cart.begin(); it != this->m_shopping_cart.end(); it++){
        cout << "菜名：" << it->get_dish().get_name() << " 数量：" << it->get_num() << endl;
    }
    cout << "总价：" << m_totle_price << endl;
    cout << "---------------------------------------------"<<endl;

    
    char tmp[100];
    memset(tmp, 0, sizeof(tmp));
    for(auto it = this->m_shopping_cart.begin(); it != this->m_shopping_cart.end(); it++){

        sprintf(tmp, "菜名：%s 数量：%d\n", it->get_dish().get_name().c_str(), it->get_num());
        tcp.sendMsg(string(tmp, sizeof(tmp)));
        memset(tmp,0,sizeof(tmp));
        usleep(300);
    }
}

void MainPage::order_clear(){
    m_shopping_cart.clear();
    m_totle_price = 0;
}

void MainPage::shop_car_interface(){
    Bmp small_interface_bmp("./res/interfaces/small_interface.bmp");
    small_interface_bmp.show(6,65);
    int frameToLcdPosX = 6;
    int frameToLcdPosY = 65;
    int i = 0;
    char str[5];

    std::list<ShopDish>::iterator it;
    for(it = m_shopping_cart.begin(); it != m_shopping_cart.end(); ++it){
        //输出菜名
        font_show((*it).get_dish().get_name().c_str(),26,170,30,0xffffffff,0,0,0x00000000,frameToLcdPosX,frameToLcdPosY+30*i);
        //输出菜的数量
        sprintf(str,"%d",(*it).get_num());
        font_show(str,26,170,30,0xffffffff,0,0,0x00000000,frameToLcdPosX+170,frameToLcdPosY+30*i);
        i++;
    }
    // int x, y;
    // Touch touch;
    // while(1){
    //     touch.wait(x, y);
    //     if(SHOP_CAR_BUTTON){

    //     }
    // }
}

void MainPage::tcp_init(){
    //创建通信的套接字
    // TcpSocket tcp;

    //连接服务器IP port
    int ret = tcp.connectToHost("192.168.1.82", 20000);
    if(ret == -1){
        return;
    }
}

MainPage::~MainPage(){}